package com.yc.bbs.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.bbs.dao.TblBoardMapper;
import com.yc.bbs.entity.TblBoard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
 * <p>
 * BBS论坛-板块表-13 前端控制器
 * </p>
 *
 * @author k
 * @since 2024-11-25
 */
@RestController
@RequestMapping("/tblBoard")
public class TblBoardController {
    @Autowired
    TblBoardMapper tblBoardMapper;
    ObjectMapper objectMapper=new ObjectMapper();
    @GetMapping("query1")
    List<TblBoard> query1(){
        return tblBoardMapper.selectByPid(0);
    }
    @GetMapping("query")
    Object query(){
        try(Jedis jedis=new Jedis("localhost",7004)){
            String key="bbs_home_bords";
            if(jedis.exists(key)){
                String json=jedis.get(key);
                List list=objectMapper.readValue(json,List.class);
                return list;
            }else{
                List<TblBoard> boards=tblBoardMapper.selectByPid(0);
                String json=objectMapper.writeValueAsString(boards);
                jedis.set(key, json);
                return boards;
            }
        } catch (JsonMappingException e) {
            throw new RuntimeException(e);
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    @RequestMapping("queryById")
    TblBoard queryById(int id){
        return tblBoardMapper.selectById(id);
    }
}
